#!/usr/bin/env bash
#DONT ULTRAPASS '|'
#
#-----------HEADER------------------------------------------------------------|
#AUTOR
#  Jefferson Rocha <lrcjefferson@gmail.com>
#
#PROGRAM
#  MYIP
#
#DESCRIPTION
#  Imprimi ip externo e Geolocalização, com uso do curl#
#  print external ip and Geolocation, with curl
#
#CHANGELOG
#  V1.2 - Jefferson Rocha
#   -Added server v4.ifconfig.co and left default server
#   -ifconfig.me stayed as a safeguard if the first one fails!
#
#  V1.3 - Jefferson Rocha
#   -Added more servers faster for
#   dry ip query.
#   -Added support for geolocation.
#   -Now the program is bilingual (BR and ENG)
#   -Change all vars for eng suport.
#   -Added conference in -geoip, param is null? exit.
#
#-----------------------------------------------------------------------------|


#-----TEST-------------#    

# Obtain lang system;
if [[ "$LANG" = "pt_BR.UTF-8" ]]; then
    lang="br"
elif [[ "$LANG" = "pt_BR.utf8" ]]; then
    lang="br"
elif [[ "$LANG" = "pt_BR.utf-8" ]]; then
    lang="br"
else
    lang="us"
fi

# Curl exist?
if [[ "$lang" = "br" ]]; then
    [[ $(which curl) ]] || { echo "Necessita do curl para continuar" ; exit 1 ;}
else
    [[ $(which curl) ]] || { echo "Need curl for continue" ; exit 1 ;}
fi

#----------------------#



#------VARS------------#
version="1.3"

#--Colors
red="\033[31;1m"
cyan="\033[36;1m"
end="\033[m"

#--List servers for show ip
server1="icanhazip.com"                  # Fast link
server2="v4.ifconfig.co"                 # Fast link
#server3="http://whatismyip.akamai.com"  # Medium Link
#server2="ifconfig.me"                   # Slow link

#--Server Geoip
server_geoip="ipinfo.io"

#----------------------#



#-----FUNCTIONS--------#
pure_ip(){
    echo -e "\nWait..."
    exec_=$(if ! curl -s $server1; then curl -s $server2 ; fi)     # Server 1 fail? ok... run server2
    echo -e "\n${red}Seu External IP is:${cyan} ${exec_}${end}\n"
}

ip_puro(){
    echo -e "\nAguarde..."
    local exec_=$(if ! curl -s $server1; then curl -s $server2 ; fi)     # Server 1 fail? ok... run server2
    echo -e "\n${red}Seu IP Externo é:${cyan} ${exec_}${end}\n"
}


geo_ip(){
    
    local show_me=$(curl -s ${server_geoip}/${ip})
    echo "$show_me" | \
    sed 's/"//g ; s/{// ; s/}// ; s/,//g' | tr a-z A-z

}


geo_ip_br(){

    local show_me=$(curl -s ${server_geoip}/${ip})   # Magic here.
    echo "$show_me" | \
    sed 's/"//g ; s/city/Cidade/ ; s/region/REGIÃO/ ; s/country/Pais/ ; s/loc/Localização/ ; s/{//g ; s/}// ; s/,//g' | tr a-z A-z
}

#----------------------#


#--Start
if [[ "$lang" = "br" ]]; then
    case $1 in
        -ip) ip_puro ;; # Call Func
        -geoip)
            shift
            ip="$1"
            [[ -z "$ip" ]] && { echo "Passe um ip como parâmetro, examplo 'myip -geoip 8.888.8.8'" ; exit 1 ;}
            geo_ip_br ;; # Call Func
        *) echo "Usage" ;;
    esac
else
    case $1 in
        -ip) pure_ip ;; # Call Func
        -geoip)
            shift
            ip="$1"
            [[ -z "$ip" ]] && { echo "Pass ip parameter, example 'myip -geoip 8.888.8.8'" ; exit 1 ;}
            geo_ip ;; # Call Func
    esac
fi



